package org.elzat.healthboxgatewayapp.service.implV1;

import lombok.AllArgsConstructor;
import org.elzat.healthboxgatewayapp.credentials.HealthBoxUserRole;
import org.elzat.healthboxgatewayapp.dto.HealthBoxUserEntity;
import org.elzat.healthboxgatewayapp.dto.HealthBoxUserVO;
import org.elzat.healthboxgatewayapp.repository.HealthBoxUserRepository;
import org.elzat.healthboxgatewayapp.service.HealthBoxUserQueryService;
import org.elzat.healthboxgatewayapp.util.DataConversionUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@AllArgsConstructor
public class HealthBoxUserQueryServiceV1 implements HealthBoxUserQueryService {

    private final HealthBoxUserRepository userRepo;

    @Override
    public List<HealthBoxUserVO> getAllUsers() {
        List<HealthBoxUserEntity> entities = userRepo.findAll();
        return DataConversionUtil.convert(entities);
    }

    @Override
    public Optional<HealthBoxUserVO> getUserByUserPhone(String userPhone) {
        Optional<HealthBoxUserEntity> maybe = userRepo.findByUserPhone(userPhone);
        return maybe.map(DataConversionUtil::convert);
    }

    @Override
    public Optional<HealthBoxUserVO> getUserByIdNumber(String idNumber) {
        Optional<HealthBoxUserEntity> maybe = userRepo.findByIdNumber(idNumber);
        return maybe.map(DataConversionUtil::convert);
    }

    @Override
    public Optional<HealthBoxUserVO> getUserByUserPhoneAndIdNumber(String userPhone, String idNumber) {
        Optional<HealthBoxUserEntity> maybe = userRepo
                .findByUserPhoneAndIdNumber(userPhone, idNumber);
        return maybe.map(DataConversionUtil::convert);
    }

    @Override
    public List<HealthBoxUserVO> getAllByRole(HealthBoxUserRole role) {
        List<HealthBoxUserEntity> entities = userRepo.findAllByRole(role.name());
        return DataConversionUtil.convert(entities);
    }

    @Override
    public Optional<HealthBoxUserVO> getUserByUserPhoneAndRole(String userPhone, String role) {
        Optional<HealthBoxUserEntity> maybe = userRepo.findByUserPhoneAndRole(userPhone, role);
        return maybe.map(DataConversionUtil::convert);
    }

    @Override
    public Optional<HealthBoxUserVO> getUserByIdNumberAndRole(String idNumber, String role) {
        Optional<HealthBoxUserEntity> maybe = userRepo.findByIdNumberAndRole(idNumber, role);
        return maybe.map(DataConversionUtil::convert);
    }
}
